//
//  MYChangePhoneNoViewController.m
//  投融保
//
//  Created by 523 on 13-12-23.
//  Copyright (c) 2013年 木叶科技. All rights reserved.
//

#import "MYChangePhoneNoViewController.h"
#import "MYWebService.h"
@interface MYChangePhoneNoViewController ()

@end

@implementation MYChangePhoneNoViewController
{
    int i;
    NSTimer *timer;
}
@synthesize txtCurrentPhone;
@synthesize title;
extern NSString *key;
extern NSString *appKey;
extern NSDictionary *userInformation ;
int prewTag ;  //编辑上一个UITextField的TAG,
float prewMoveY; //编辑的时候移动的高度

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.lblOldPhone.text=[NSString stringWithFormat:@"  %@",[userInformation valueForKey:@"mobile"]];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnReturn:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)sendCaptchaAction:(UIButton *)sender {
    
    NSString *strAppMobileService=[NSString stringWithFormat:@"service=appMobileService&mobile=%@&business=cellphone",self.txtCurrentPhone.text];
    MYWebService *service=[MYWebService new];
    
    MBProgressHUD *mbHud = [[MBProgressHUD alloc] initWithView:self.view];
    
    mbHud.labelText = @"正在加载";
    
    [self.view addSubview:mbHud];
    
    mbHud.delegate =self;
    
    [mbHud showAnimated:YES whileExecutingBlock:^{
        
        //z菊花转动的时候 开始加载数据
        [service getDataSynWithKey:key encryptedString:strAppMobileService appKey:nil];
        
    } completionBlock:^{
        //执行加载数据完成后的操作。
        NSDictionary *dict=[service.profileDataSyn objectForKey:@"result"];
        i=60;
        if ([[dict objectForKey:@"code"] intValue]==1) {
            timer =[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countdown:) userInfo:nil repeats:YES];
        }
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:[dict objectForKey:@"message"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }];
    
    //    NSLog(@"重发短信返回消息:%@",dict);
    
}

- (void)countdown:(NSTimer *)atimer
{
    i--;
    [self.btnSendCaptcha setTitle:[NSString stringWithFormat:@"获取验证码（%d）",i] forState:UIControlStateNormal];
    if (i<=0) {
        [timer invalidate];
        [self.btnSendCaptcha setTitle:@"获取验证码（重发）" forState:UIControlStateNormal];
    }
}


- (IBAction)btnSubmitAction:(UIButton *)sender {
    if ([self.txtCaptcha.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]].length>0)
    {
        [timer invalidate];
        [self.btnSendCaptcha setTitle:@"获取验证码（重发）" forState:UIControlStateNormal];
        //    NSString *strAppBindingMobileService =[NSString stringWithFormat:@"service=appBindingMobileService &verifyCode=%@&mobile=%@&business=CELLPHONE", self.txtCaptcha.text,self.txtCurrentPhone.text];
        NSString * strAppBindingMobileService=[NSString stringWithFormat:@"service=appBindingMobileService&verifyCode=%@&mobile=%@&business=CELLPHONE",self.txtCaptcha.text,self.txtCurrentPhone.text];
        MYWebService *service=[MYWebService new];
        
        MBProgressHUD *mbHud = [[MBProgressHUD alloc] initWithView:self.view];
        
        mbHud.labelText = @"正在发送";
        
        [self.view addSubview:mbHud];
        
        [mbHud showAnimated:YES whileExecutingBlock:^{
            
            //z菊花转动的时候 开始加载数据
            [service getDataSynWithKey:key encryptedString:strAppBindingMobileService appKey:appKey];
            
        } completionBlock:^{
            
            //执行加载数据完成后的操作。
            NSDictionary *dictChangePhone=[service.profileDataSyn objectForKey:@"result"];
            int code=[[dictChangePhone valueForKey:@"code"] intValue];
            
            switch (code) {
                case 1:
                {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"手机修改成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alert show];
                }
                    break;
                case 0:
                {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"验证码校验失败" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    
                    [alert show];
                }
                    break;
                    
                default:
                {
                    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"%@",[dictChangePhone valueForKey:@"message"]] delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                    [alert show];
                }
                    
                    break;
            }
            
        }];
    }else
    {
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:nil message:@"验证码不能为空" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
    }
    
    
    //    NSLog(@"修改结果：%@",dictChangePd);
    
    
    
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self dismissViewControllerAnimated:YES completion:nil];
//            [self dismissModalViewControllerAnimated:YES];
            break;
            
        default:
            break;
    }
}



//
//#pragma mark +++++++++键盘+++++++++++
///**
// 开始编辑UITextField的方法
// */
//-(void) textFieldDidBeginEditing:(UITextField *)textField
//{
//
//    CGRect textFrame =  textField.frame;
//    float textY = textFrame.origin.y+textFrame.size.height;
//    float bottomY = self.touZiScroll.frame.size.height-textY;
//    if(bottomY>=260)  //判断当前的高度是否已经有216，如果超过了就不需要再移动主界面的View高度
//    {
//        prewTag = -1;
//        return;
//    }
//    prewTag = textField.tag;
//    float moveY;
//    if (SCREEN_HEIGHT>480) {
//        moveY = 260-bottomY;
//    }else
//    {
//        moveY = 300-bottomY;
//    }
//
//    prewMoveY = moveY;
//    [UIView animateWithDuration:0.25 animations:^{
//        [self.touZiScroll setContentOffset:CGPointMake(0, prewMoveY) animated:YES];
//    }];
//
//}
//
///**
// 结束编辑UITextField的方法，让原来的界面还原高度
// */
//-(void)textFieldDidEndEditing:(UITextField *)textField
//{
//    //设置大写金额
//    if (textField==self.touZiMoney) {
//        self.sureTouZiMoney.text=[MoneyString moneyStringWithDigitStr:self.touZiMoney.text];
//    }
//
//    if(prewTag == -1) //当编辑的View不是需要移动的View
//    {
//        return;
//    }
//    float moveY ;
//
//    if(prewTag == textField.tag) //当结束编辑的View的TAG是上次的就移动
//    {   //还原界面
//        moveY =  prewMoveY;
//        [self.touZiScroll setContentOffset:CGPointMake(0, moveY)];
//    }
//
//
//
//
//    
//}
#pragma mark-   -----------状态栏底色------
- (UIStatusBarStyle)preferredStatusBarStyle
{
    [self.view setBackgroundColor:[UIColor blackColor]];
    return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden
{
    return NO;
}
@end
